'use strict';

const fs = require('fs');
const { BaseController } = require('@/core');

const suffixes = [
  { suffix: '.js', type: 'text/javascript' },
  { suffix: '.css', type: 'text/css' },
  { suffix: '.ico' },
  { suffix: 'png' },
];
class HomeController extends BaseController {
  async index() {
    const { ctx } = this;
    const resolve = path => ctx.helper.resolve('app/view', path);
    const url = decodeURIComponent(ctx.request.url.substring(1));
    try {
      const data = fs.readFileSync(resolve(url));
      for (const { suffix, type } of suffixes) {
        if (url.endsWith(suffix)) {
          if (type) ctx.response.type = type;
          break;
        }
      }
      ctx.body = data;
    } catch (err) {
      ctx.response.type = 'html';
      ctx.body = fs.readFileSync(resolve('index.html'));
    }
  }
  // async index() {
  //   await this.result(Promise.resolve('bestlyg server ： 2022/2/12 2153 '));
  // }
}

module.exports = HomeController;
